#pragma once
#include <Windows.h>
#include <map>
#include "../Handle/Handle.hpp"
#include "../Handle/HandleMgr.hpp"
#include "../../Tool/Singleton.h"

namespace Kriko
{
	typedef HANDLE OsHandle;
	struct TagFile {};
	typedef Handle<TagFile> FileHandle;
	class FileSystem
	{
	private:
		struct File
		{
			const char* m_FileName;
			OsHandle m_Handle;
			bool Load(const char* p_FileName);
			bool UnLock();
			void WirteToFile(const char* p_Text);
			char* ReadFormFile();
		};
		typedef HandleMgr<File, FileHandle> FileMgr;
		struct isString_Less
		{
			const bool operator() (const char* l, const char* r) const
			{
				return ::stricmp(l,r)<0;
			}
		};
		typedef std::map<const char*, FileHandle,isString_Less> NameIndex;
		typedef std::pair<NameIndex::iterator, bool> NameIndexIterator;

		NameIndex m_NameIndex;
		FileMgr m_FileMgr;

	public:
		FileSystem() {}
		~FileSystem();

		FileHandle GetFileRes(const char* p_FileName);
		void DeleteFileRes(FileHandle p_Handle);

		const char* GetFileName(FileHandle p_Handle) { return m_FileMgr.Dereference(p_Handle)->m_FileName; }
		OsHandle GetFile(FileHandle p_Handle) { return m_FileMgr.Dereference(p_Handle)->m_Handle; }
		void WriteToFile(FileHandle p_Handle, const char* p_Text) { m_FileMgr.Dereference(p_Handle)->WirteToFile(p_Text); };
		char* ReadFormFile(FileHandle p_Handle) { return m_FileMgr.Dereference(p_Handle)->ReadFormFile(); }
	};
}